package com.wechat.android.app.ui.bottom;


import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.easemob.cases.MessageHelper;
import com.hyphenate.chat.EMClient;
import com.wechat.android.app.R;
import com.wechat.android.app.db.UserHelper;
import com.wechat.android.app.entity.UserInfoModel;
import com.wechat.android.app.global.Constants;
import com.wechat.android.app.rxbus.RxBus;
import com.wechat.android.app.rxbus.Subscribe;
import com.wechat.android.app.rxbus.ThreadMode;
import com.wechat.android.app.rxbus.event.CommonEvent;
import com.wechat.android.app.ui.BaseFragment;
import com.wechat.android.app.ui.login.ForgetPasswordActivity;
import com.wechat.android.app.ui.login.LoginActivity;
import com.wechat.android.app.ui.login.UpdateUserPhoneActivity;
import com.wechat.android.app.ui.userinfo.UserInfoActivity;
import com.wechat.android.app.utils.ActivityHelper;
import com.wechat.android.app.utils.MyLog;
import com.wechat.android.app.utils.SharedPreferencesUtils;
import com.wechat.android.app.utils.UniversalDialog;
import com.wechat.android.app.utils.glide.GlideCacheUtil;
import com.wechat.android.app.utils.glide.GlideUtils;
import com.wechat.android.app.widget.CustomFontTextView;
import com.wechat.android.app.widget.RoundImageView;
import com.wechat.android.app.widget.SwitchView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by yang on 2017/11/23.
 * 我的
 */

public class PersonFragment extends BaseFragment {
    @BindView(R.id.img_user_portrait)
    RoundImageView imgUserPortrait;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.ll_update_password)
    LinearLayout llUpdatePassword;
    @BindView(R.id.ll_update_bind_phone)
    LinearLayout llUpdateBindPhone;
    @BindView(R.id.tv_cache)
    TextView tvCache;
    @BindView(R.id.ll_clean_cache)
    LinearLayout llCleanCache;
    @BindView(R.id.btn_exit_login)
    CustomFontTextView btnExitLogin;
    @BindView(R.id.tv_update_password)
    CustomFontTextView tvUpdatePassword;
    @BindView(R.id.tv_update_bind_phone)
    CustomFontTextView tvUpdateBindPhone;
    @BindView(R.id.tv_clean_cache)
    CustomFontTextView tvCleanCache;
    @BindView(R.id.tv_switch_input)
    CustomFontTextView tvSwitchInput;
    @BindView(R.id.switch_view)
    SwitchView switchView;
    Unbinder unbinder;

    private UserInfoModel userInfoModel = null;

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_person;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText("\uD81B\uDF1E\uD81B\uDFAE\uD81B\uDF00\uD81B\uDFC4\uD81B\uDFC0");
        tvUpdatePassword.setText("\uD81B\uDF1A\uD81B\uDF60\uD81B\uDF1A\uD81B\uDF51\uD81B\uDF61\uD81B\uDF00\uD81B\uDF7A\uD81B\uDF04\uD81B\uDF7B\uD81B\uDF50\uD81B\uDF0A\uD81B\uDFA8\uD81B\uDF08\uD81B\uDF7A");
        tvUpdateBindPhone.setText("\uD81B\uDF1A\uD81B\uDF60\uD81B\uDF1A\uD81B\uDF51\uD81B\uDF61\uD81B\uDF3A\uD81B\uDFC9\uD81B\uDF2F\uD81B\uDFA9\uD81B\uDF1E\uD81B\uDF6A\uD81B\uDF21\uD81B\uDF51\uD81B\uDF79\uD81B\uDF3B\uD81B\uDFC4\uD81B\uDFC0");
        tvCleanCache.setText("\uD81B\uDF2F\uD81B\uDF51\uD81B\uDFC4\uD81B\uDF26\uD81B\uDF61\uD81B\uDF1A\uD81B\uDF58\uD81B\uDF3A\uD81B\uDFBD");
        tvSwitchInput.setText("\uD81B\uDF2F\uD81B\uDFC4\uD81B\uDFBE\uD81B\uDF43\uD81B\uDFA0\uD81B\uDF51\uD81B\uDF04\uD81B\uDFD4\uD81B\uDF50\uD81B\uDF0A\uD81B\uDFA8/\uD81B\uDF2F\uD81B\uDFC4\uD81B\uDFBE\uD81B\uDF00\uD81B\uDF51\uD81B\uDFBB\uD81B\uDF3D\uD81B\uDFD4\uD81B\uDF50\uD81B\uDF0A\uD81B\uDFA8");
        btnExitLogin.setText("\uD81B\uDF3D\uD81B\uDFA0\uD81B\uDF0A\uD81B\uDF60\uD81B\uDF04\uD81B\uDF7B\uD81B\uDF2F\uD81B\uDF58");
        RxBus.getDefault().register(this);
        tvCache.setText(GlideCacheUtil.getInstance().getCacheSize());
        userInfoModel = UserHelper.getInstance().getUserInfoModel(getActivity());
        if (userInfoModel != null) {
            setUserInfo(userInfoModel);
        }


        if ((int) SharedPreferencesUtils.getParam(getActivity(), UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_IS_NOTICE_MESSAGE, 1) == 1) {
            switchView.setState(true);
        } else {
            switchView.setState(false);
        }
        switchView.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
            @Override
            public void toggleToOn() {
                switchView.toggleSwitch(true);
                SharedPreferencesUtils.setParam(getActivity(), UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_IS_NOTICE_MESSAGE, 1);

            }

            @Override
            public void toggleToOff() {
                switchView.toggleSwitch(false);
                SharedPreferencesUtils.setParam(getActivity(), UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_IS_NOTICE_MESSAGE, 0);

            }
        });




    }

    @Override
    protected void updateViews() {

    }


    @OnClick({R.id.img_user_portrait, R.id.ll_update_password, R.id.ll_update_bind_phone, R.id.ll_clean_cache, R.id.btn_exit_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_user_portrait:
                startActivity(new Intent(getActivity(), UserInfoActivity.class));
                break;
            case R.id.ll_update_password:
                Intent ti = new Intent(getActivity(), ForgetPasswordActivity.class);
                ti.putExtra("title", "\uD81B\uDF1A\uD81B\uDF60\uD81B\uDF1A\uD81B\uDF51\uD81B\uDF61\uD81B\uDF00\uD81B\uDF7A\uD81B\uDF04\uD81B\uDF7B\uD81B\uDF50\uD81B\uDF0A\uD81B\uDFA8\uD81B\uDF08\uD81B\uDF7A");
                startActivity(ti);
                break;
            case R.id.ll_update_bind_phone:
                startActivity(new Intent(getActivity(), UpdateUserPhoneActivity.class));
                break;
            case R.id.ll_clean_cache:
                new cleanCacheAsyncTask().execute();
                break;
            case R.id.btn_exit_login:
                ExitLoginDialog("\uD81B\uDF3D\uD81B\uDFA0\uD81B\uDF0A\uD81B\uDF60\uD81B\uDF04\uD81B\uDF7B\uD81B\uDF2F\uD81B\uDF58", "\uD81B\uDF0A\uD81B\uDF60\uD81B\uDF16\uD81B\uDF60\uD81B\uDF26\uD81B\uDFC4\uD81B\uDF37\uD81B\uDFC3\uD81B\uDF07\uD81B\uDF73\uD81B\uDF3A\uD81B\uDFBD\uD81B\uDF04\uD81B\uDFD4\uD81B\uDF21\uD81B\uDFBB\uD81B\uDF33\uD81B\uDFD1,\uD81B\uDF43\uD81B\uDFBB\uD81B\uDF1E\uD81B\uDFD4\uD81B\uDF00\uD81B\uDF7A\uD81B\uDF0A\uD81B\uDFD6\uD81B\uDF2F\uD81B\uDFDF\uD81B\uDFD9\uD81B\uDF50\uD81B\uDF37\uD81B\uDFD1\uD81B\uDF10\uD81B\uDFA9",
                       "\uD81B\uDF3B\uD81B\uDFD4\uD81B\uDF0A\uD81B\uDF58","\uD81B\uDF07\uD81B\uDF73\uD81B\uDF3A\uD81B\uDFBD");
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    // 清除缓存
    class cleanCacheAsyncTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mMProgressDialog.showNoText();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            try {
                GlideUtils.clearDiskCache(getActivity());
            } catch (Exception e) {

            }
            return null;

        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            tvCache.setText("0.0KB");
            mMStatusDialog.show("\uD81B\uDF26\uD81B\uDFA9\uD81B\uDF1A\uD81B\uDF58\uD81B\uDF2F\uD81B\uDF51\uD81B\uDFC4\uD81B\uDF28\uD81B\uDF51\uD81B\uDFAE", null);
            mMProgressDialog.dismiss();
        }

    }

    private void ExitLoginDialog(String title, String msg, String sure, String cancle) {
        new UniversalDialog(getActivity()).builder().setTitle(title)
                .setMsg(msg)
                .setPositiveButton(sure, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SharedPreferencesUtils.remove(UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_AUTH_KEY_STORAGE);
                        SharedPreferencesUtils.remove(UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_USER_INFO_MODEL);
                        ActivityHelper.getInstance().finishAllActivity();
                        UserHelper.getInstance().setUserInfoModel(getActivity(), null);
                        UserHelper.getInstance().setIsLogined(false);
                        EMClient.getInstance().logout(true);
                        Intent ti = new Intent(getActivity(), LoginActivity.class);
                        startActivity(ti);

                    }
                }).setNegativeButton(cancle, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        }).show();
    }

    private void setUserInfo(UserInfoModel userInfoModel) {
        tvUserName.setText(userInfoModel.getName());
        if (!TextUtils.isEmpty(userInfoModel.getLogo())) {
            GlideUtils.loadImage(getActivity(), userInfoModel.getLogo(), imgUserPortrait, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }

                @Override
                public void onLoadingError(String source, Exception e) {

                }
            }, R.mipmap.icon_miaowen, R.mipmap.icon_miaowen);
        } else {
            imgUserPortrait.setImageResource(R.mipmap.icon_miaowen);
        }
    }

    @Subscribe(code = Constants.RXBUS_CODE_REFRESH_USERINFO, threadMode = ThreadMode.MAIN)
    public void refreshUserInfo(CommonEvent event) {
        MyLog.e("yang", "msg==" + event.getMsg());
        UserInfoModel userInfoModel = UserHelper.getInstance().getUserInfoModel(getActivity());
        setUserInfo(userInfoModel);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unRegister(this);
    }
}
